package com.yang.service.wximpl;

import com.yang.bean.MarketCategory;
import com.yang.bean.MarketCategoryExample;
import com.yang.bean.yvo.Category;
import com.yang.bean.yvo.wx.CatalogBean;
import com.yang.bean.yvo.wx.CurrentBean;
import com.yang.mapper.MarketCategoryMapper;
import com.yang.service.wx.WXCataLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @author: 杨庆
 * @since: 2022.9.9 15:06
 * @description:
 */
@Service
public class WXCataLogServiceImpl implements WXCataLogService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Override
    public CatalogBean selectList() {
        CatalogBean catalogBean = new CatalogBean();
        //TODO 查询所有的一级类目
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L1");
        List<MarketCategory> list = marketCategoryMapper.selectByExample(example);
        // TODO 取出第一个的
        MarketCategory marketCategory = list.get(0);
        catalogBean.setCurrentCategory(marketCategory);
        //TODO 根据父类的id，查询所有的子类
        Integer id = marketCategory.getId();
        MarketCategoryExample example1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andPidEqualTo(id);
        criteria1.andLevelEqualTo("L2");
        List<MarketCategory> list1 = marketCategoryMapper.selectByExample(example1);

        catalogBean.setCurrentSubCategory(list1);
        catalogBean.setCategoryList(list);
        return catalogBean;
    }

    @Override
    public CurrentBean selectById(Integer id) {
        CurrentBean catalogBean = new CurrentBean();
        //TODO 查询父类
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L1");
        criteria.andIdEqualTo(id);
        List<MarketCategory> list = marketCategoryMapper.selectByExample(example);
        //TODO 根据父类的id，查询所有的子类
        MarketCategoryExample example1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andPidEqualTo(id);
        criteria1.andLevelEqualTo("L2");
        List<MarketCategory> list1 = marketCategoryMapper.selectByExample(example1);
        catalogBean.setCurrentSubCategory(list1);
        catalogBean.setCurrentCategory(list.get(0));
        return catalogBean;
    }
}
